# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import sqlite3


class LianjiaproPipeline(object):
    def open_spider(self, spider):
        self.con = sqlite3.connect('db.sqlite3')
        self.cu = self.con.cursor()

    def process_item(self, item, spider):
        insert_sql = 'insert into main.ershou_hosemsg (' \
                     'title,communityname,roomtype,area,orientation,unitprice,price,areaname,houserecord,rid,longitude,latitude)' \
                     ' values ("{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}")'.format(
            item['title'], item['communityname'], item['roomtype'], item['area'], item['orientation'],
            item['unitprice'],
            item['price'], item['areaname'], item['houserecord'], item['rid'], item['longitude'], item['latitude'])

        self.cu.execute(insert_sql)

        self.con.commit()

        print(item)
        return item

    def spider_close(self, spider):
        self.con.close()
